//
//  UIImage + Extension.swift
//  Weibo - Swift
//
//  Created by user on 16/4/26.
//  Copyright © 2016年 user. All rights reserved.
//

import UIKit


extension UIImage{
    
    func scaleToWidth(width:CGFloat)->UIImage{
        
        if self.size.width < width{
            
            return self
        }
        
        // 20 100
        // 40 200
        let height = width/self.size.width * self.size.height
        
        let rect = CGRectMake(0, 0, width, height)
        
        UIGraphicsBeginImageContext(rect.size)
        
        self.drawInRect(rect)
        
        let result = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        
        return result
    }
    
}